package servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import dao.UserDao;
import dao.impl.UserDaoImpl;
import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

/**
 * Servlet implementation class SywUserLoginServlet
 * 
 * 用户登录Servlet
 */
public class SywUserLoginServlet extends HttpServlet {
	private UserDao ud = new UserDaoImpl();
	private UserService us = new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		  response.setContentType("applition/json;charset=utf-8");
		  User user = new User();	  
		  String userName = request.getParameter("userName");
		  String userPwd = request.getParameter("userPwd");		  
		  user.setUsername(userName);
		  user.setPassword(userPwd);	
		  
		  boolean flag = us.queryUserByLoginNameAndPassword(userName, userPwd);
		  if (flag) {
		   request.getSession().setAttribute("user", user);
		   request.getRequestDispatcher("Index.jsp").forward(request, response);
		   
		  }else{		   
		   response.sendRedirect("Login.jsp");
		  }

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
